﻿using DevComponents.DotNetBar.SuperGrid;
using SDATA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0123 : Form
    {
        public frm0123()
        {
            InitializeComponent();
        }

        string sql = "";
        DataTable dt_Grid1 = new DataTable();
        string _GroupID = "";

        private void frm0123_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void frm0123_Shown(object sender, EventArgs e)
        {
            loadDATA();
        }

        public void loadDATA()
        {
            sql = "Select * from tb0070 Order by GroupID";
            dt_Grid1 = FRM.GetDataTable(sql);
            GridDB1.PrimaryGrid.DataSource = dt_Grid1;
            foreach (GridColumn gc in GridDB1.PrimaryGrid.Columns)
                gc.FilterMatchType = FilterMatchType.Wildcards;
        }

        private void btnAddGroup_Click(object sender, EventArgs e)
        {
            mmAdd();
        }

        void mmAdd()
        {
            Form frm = new frm0128();
            frm.ShowDialog();
        }

        void mmEdit()
        {
            Form frm = new frm0128(false, false, _GroupID);
            frm.ShowDialog();
        }

        void mmDelete()
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1)
            {
                SDLL.DLL02.Alert("Bạn phải chọn dòng cần xóa!");
                return;
            }
            if (!SDLL.DLL02.Confirm("Bạn có chắc muốn xóa?", "Xác nhận")) { return; }

            sql = string.Format("SELECT TOP 1 1  From tb0071  Where GroupID ='{0}'",_GroupID);
            DataTable dt_check_tb0071 = FRM.GetDataTable(sql);
            if(dt_check_tb0071.Rows.Count>0)
            {
                SDLL.DLL02.Alert("Bạn phải gỡ bỏ thành viên trong nhóm trước khi xóa nhóm");
                return;
            }

            sql = string.Format("DELETE tb0070 WHERE GroupID='{0}'", _GroupID);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                sql = string.Format(" Delete tb0075 WHERE GroupID = '{0}'", _GroupID);
                DATA.ExecuteNonQuery(sql, FRM.ConnectString, false);
                sql = string.Format("EXEC pr0093 '{0}', 'D'", _GroupID);
                DATA.ExecuteNonQuery(sql, FRM.ConnectString, false);

                loadDATA();
            }
            else
            {
                SDLL.DLL02.Alert("Xóa dữ liệu không thành công");
            }
        }

        void mmRefresh()
        {
            loadDATA();
        }

        void mmInfo()
        {
            string tn = "tb0070";
            string ws = string.Format("WHERE GroupID='{0}'", _GroupID);
            Form frm = new frm0067(tn, ws);
            frm.ShowDialog();
        }

        void mmMemberGroup()
        {
            Form frm = new frm0126(_GroupID);
            frm.ShowDialog();
        }

        private void GridDB1_SelectionChanged(object sender, DevComponents.DotNetBar.SuperGrid.GridEventArgs e)
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1) { return; }
            GridRow gr = (GridRow)GridDB1.PrimaryGrid.GetSelectedRows()[0];
            DataRowView drv = (DataRowView)gr.DataItem;
            DataTable dt = dt_Grid1.Clone();
            dt.ImportRow(drv.Row);
            _GroupID = dt.Rows[0]["GroupID"].ToString();
        }

        private void GridDB1_RowDoubleClick(object sender, GridRowDoubleClickEventArgs e)
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1) { return; }
            mmEdit();
        }

        private void btnMemberGroup_Click(object sender, EventArgs e)
        {
            mmMemberGroup();
        }

        private void mMemberGroup_Click(object sender, EventArgs e)
        {
            mmMemberGroup();
        }

        private void mAdd_Click(object sender, EventArgs e)
        {
            mmAdd();
        }

        private void mEdit_Click(object sender, EventArgs e)
        {
            mmEdit();
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
            mmDelete();
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            mmRefresh();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            mmInfo();
        }




    }
}
